package com.tools.commons.model;

import lombok.Data;

@Data
public class RequestInfo {
    private String requestId = "-";
    private String time = "-";
    private String ip = "-";
    private String userName = "-";
    private String host = "-";
    private String service = "-";
    private String method = "-";
    private String path = "-";
    private String reqContentLength = "-";
    private int respStatus = 0;
    private String respContentType = "-";
    private int respContentLength = 0;
    private long respTime = 0;
    private String xForwardedFor = "-";
    private String referer = "-";
    private String userAgent = "-";
    private String userData = "-";

    @Override
    public String toString() {
        return String.format(
                "%s|%s|%s|%s|%s|%s|%s|%s|%s|%d|%s|%d|%d.%09d|%s|%s|%s|%s"
                , convert(requestId)
                , convert(time)
                , convert(ip)
                , convert(userName)
                , convert(host)
                , convert(service)
                , convert(method)
                , convert(path)
                , convert(reqContentLength)
                , respStatus
                , convert(respContentType)
                , respContentLength
                , respTime / 1000000000
                , respTime % 1000000000
                , convert(xForwardedFor)
                , convert(referer)
                , convert(userAgent)
                , convert(userData));
    }

    private String convert(String s) {
        if (s == null) {
            return "-";
        }
        return s.replace("\\", "\\\\").replace("|", "\\-").replace("\n", "\\n");
    }
}
